﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Ocmf.Forms;

namespace Ocmf.Types
{
    public class Counter:Action
    {
        public int Count { get; set; }
        private string alias = "Default Counter";
        public string Alias
        {
            get
            {
                return alias;
            }
            set
            {
                alias = value;
                Text = "Counter: " + Alias + (Increment ? " (+1)" :  " (-1)");
            }
        }

        private bool increment = false;
        public bool Increment
        {
            get
            {
                return increment;
            }
            set
            {
                increment = value;
                Text = "Counter: " + Alias + (Increment ? " (+1)" : " (-1)");
            }
        }

        public Counter()
        {
            Text = "Counter: " + Alias + (Increment ? " (+1)" : " (-1");
            ImageKey = SelectedImageKey = "counter";
        }

        public override object Clone()
        {
            var clone = base.Clone() as Counter;
            clone.Count = Count;
            return clone;
        }

        public override System.Windows.Forms.Form Editor
        {
            get { return new EditCounter(this); }
        }

        public override System.Xml.XmlElement ToXml(System.Xml.XmlDocument xmlDocument)
        {
            //create trial xml element
            var incrementCounterElement = xmlDocument.CreateElement("Counter");
            //create duration attribute
            var aliasAttribute = xmlDocument.CreateAttribute("Alias");
            aliasAttribute.InnerText = Alias;
            //create duration attribute
            var incrementAttribute = xmlDocument.CreateAttribute("Increment");
            incrementAttribute.InnerText = increment.ToString();
            //add attribute to element
            incrementCounterElement.Attributes.Append(aliasAttribute);
            incrementCounterElement.Attributes.Append(incrementAttribute);
            //return element
            return incrementCounterElement;
        }

        public override void FromXml(System.Xml.XmlNode xmlElement)
        {
            Alias = xmlElement.Attributes["Alias"].InnerText;
            Increment = bool.Parse(xmlElement.Attributes["Increment"].InnerText);
        }
    }
}
